<?php
error_reporting(E_ALL ^ E_NOTICE);
$board_name = $_REQUEST['board_name'];
$dish_name = $_REQUEST['dish_name'];
include 'db.php';
?>
<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>点餐系统</title>

    <!-- Bootstrap -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="navbar navbar-default" style="margin-bottom:20px">
    <div class="container-fluid">
        <div class="container">
            <div class="navbar-header">
                <span class="navbar-brand">点餐系统</span>
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            订单管理
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="order.php">订单列表</a></li>
                            <li><a href="order2.php">订单记录</a></li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            菜品管理 <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="add.php">添加菜品</a></li>
                            <li><a href="add2.php">菜品列表</a></li>
                            <li class="divider"></li>
                            <li><a href="add3.php">添加菜品类型</a></li>
                            <li><a href="add4.php">菜品类型列表</a></li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            餐桌管理
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="board.html">添加餐桌</a></li>
                            <li><a href="board2.php">餐桌列表</a></li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            登录管理 <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="#" data-toggle="modal" data-target="#aboutModal">关于点餐系统</a></li>
                            <li class="divider"></li>
                            <li><a href="./">修改登录信息</a></li>
                            <li><a href="#exitModal" data-toggle="modal">退出登录</a></li>
                        </ul>
                    </li>
                    <div class="modal fade" id="exitModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                                aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="myModalLabel">确定退出登录</h4>
                                </div>
                                <div class="modal-body">
                                    你确定要退出登录吗？
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" class="btn btn-default" data-dismiss="modal">确定</button>
                                    <button type="button" class="btn btn-default">取消</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </ul>
            </div>
        </div>
    </div>
</div>

<div class="container">
    <div class="row">
        <div class="col-xs-2">
            <form action="dc.php">
                <div class="input-group" style="margin-bottom:20px">
                    <input type="text" name="board_name" class="form-control" placeholder="桌号">
                    <span class="input-group-btn">
                            <button class="btn btn-default" type="submit">
                                <span class="glyphicon glyphicon-search"></span>
                            </button>
                        </span>
                </div>
            </form>

            <ul class="list-group" style="text-align: center">
                <?php
                include_once './db.php';
                $sql = "select * from board";
                if (isset($board_name)) {
                    $sql = "select * from board where `name` like '%$board_name%'";
                }
                $sqla = $conn->prepare($sql);
                $ok = $sqla->execute();
                $w = $sqla->fetchAll();
                $size = sizeof($w);

                for ($i = 0; $i < $size; $i++) {
                    $item = $w[$i];
                    ?>
                    <li class="list-group-item">
                        <a href="./dc.php?boardId=<?php echo $item['id']; ?>"><?php echo $item['name'] ?></a>
                    </li>

                <?php } ?>
            </ul>
        </div>
        <div class="col-xs-10">
            <?php

            $boardId = $_REQUEST["boardId"]; // 餐桌ID
            if (isset($boardId)) {
                // 执行 查询指定餐桌 sql
                $sql = "select * from board where id='$boardId'";
                $sqls = $conn->prepare($sql);
                $ok = $sqls->execute();
                $board = $sqls->fetch();
                // 输出 餐桌名称
                $boardName = $board['name'];
                // 根据 餐桌ID and 订单状态 查找未结账的订单
                $sql = "select * from dish_order where board_id='$boardId' and `status`='未结账'";
                $sqls = $conn->prepare($sql);
                $ok = $sqls->execute();
                $order = $sqls->fetch();
                if (empty($order)) {
                    $sql = "insert into dish_order (board_id,money,create_date,`status`) values($boardId,0,now(),'未结账')";
                    $sqla = $conn->prepare($sql);
                    $ok = $sqla->execute();
                }
                // 没有未结账的订单
                //   创建一个新的订单
                $ok = $sqls->execute();
                $order = $sqls->fetch();
                $o_id = $order['id'];
                $sql = "select * from order_item where order_id=$o_id";
                $sqlh = $conn->prepare($sql);
                $ok = $sqlh->execute();
                $dishs = $sqlh->fetchAll();
                // 1  10.00 1   [1,10.00,1]
                // 2   5.00 1   [2, 5.00,1]
                // 3  20.00 1   [3,20.00,1]
                // $a= [
                //      0=[1,10.00,1],
                //      1=[2, 5.00,1],
                //      2=[3,20.00,1]
                //  ]
                // $a[1]=[2, 5.00,1]
                // $a[1][1]=5.00;
                $total = 0.00;
                for ($i = 0; $i < count($dishs); $i++) {
                    $total += $dishs[$i]['price'];
                }
                // 根据订单ID 查询 订单对应的所有菜品  冗余


            }
            ?>
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="panel panel-primary" style="margin-bottom:20px">
                    <div class="panel-heading" role="tab" id="headingOne">
                        <h4 class="panel-title glyphicon glyphicon-<?php echo empty($boardName) ? 'remove' : 'ok'; ?>">
                            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"
                               aria-expanded="true" aria-controls="collapseOne">
                                <?php echo isset($boardName) ? $boardName : '没有选择餐桌' ?>
                            </a>
                        </h4>
                        <span style="text-align: right">
                            <a href="jz.php?orderId=<?php echo $order['id']; ?>&boarId=<?php echo $board['id']; ?>"
                               class="btn btn-success btn-sm pull-right">
                                结账
                                <span class="badge"><?php echo isset($total) ? $total : 0; ?>元</span>
                            </a>
                        </span>
                    </div>
                    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel"
                         aria-labelledby="headingOne">
                        <div class="panel-body" style="text-align: center">
                            <?php
                            for ($i = 0; $i < count($dishs); $i++) {
                                $item = $dishs[$i];
                                // order_item  id-dish_id-order_id-price  只有菜品的id
                                // 目的： 菜品的详细信息  图片 名字 价格 ...   select * from dish where id= order_item.dish_id

                                $dish_id = $item['dish_id'];
                                $sql = "select * from dish where id=$dish_id";
                                $sqlo = $conn->prepare($sql);
                                $ok = $sqlo->execute();
                                $dish = $sqlo->fetch();
                                ?>
                                <div class="col-xs-6 col-md-3" style="text-align:center">
                                    <div class="thumbnail">
                                        <img src="<?php echo $dish['icon']; ?>">
                                        <div class="caption">
                                            <?php echo $dish['dish_name'] . " ￥" . $dish['price'] . '' ?>

                                            <a href="del.php?id=<?php echo $item['id']; ?>&boardId=<?php echo $boardId; ?>">取消</a>
                                        </div>
                                    </div>
                                </div>
                                <?php
                            }
                            ?>

                        </div>
                    </div>
                </div>
            </div>
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-lg-6">
                        <span class="glyphicon glyphicon-th-list"
                              style="margin-right:5px;">
                        </span>
                            可选菜品
                        </div>
                        <div class="col-lg-6">
                            <form action="dc.php">
                                <div class="input-group">
                                    <input type="text" name="dish_name" class="form-control" placeholder="菜名">
                                    <div class="input-group-btn">
                                        <select class="form-control">
                                            <option>全部</option>
                                            <?php
                                            $sql = "select * from dish_type order by id asc;";
                                            $sqlo = $conn->prepare($sql);
                                            $ok = $sqlo->execute();
                                            $item = $sqlo->fetchAll();
                                            for ($i = 0; $i < count($item); $i++) {
                                                ?>

                                                <option value="<?php echo $item[$i]['id'] ?>"><?php echo $item[$i]['type_name'] ?></option>
                                                <?php
                                            }
                                            ?>
                                        </select>
                                    </div>
                                    <div class="input-group-btn">
                                        <button type="submit" class="btn btn-default" style="width: 50px">
                                            <span class="glyphicon glyphicon-search"></span>
                                        </button>
                                    </div>

                                </div>
                            </form>
                        </div>
                    </div>


                </div>
                <div class="panel-body">
                    <div class="row">
                        <?php
                        $sql = "select * from dish";
                        if (isset($dish_name)) {
                            $sql = "select * from dish where `dish_name` like '%$dish_name%'";
                        }
                        $sqls = $conn->prepare($sql);
                        $ok = $sqls->execute();
                        $s = $sqls->fetchAll();
                        $size = sizeof($s);
                        for ($i = 0; $i < $size; $i++) {

                            $item = $s[$i];
                            ?>
                            <div class="col-xs-6 col-md-3" style="text-align:center">
                                <div class="thumbnail">
                                    <img src="<?php echo $item['icon']; ?>">
                                    <div class="caption">
                                        <a href="<?php echo 'order3.php?boardId=' . $boardId . '&dishId=' . $item['id']; ?>"><?php echo $item['dish_name'] . " ￥" . $item['price'] . '' ?></a>
                                        <!--<a href="order3.php?boardId=<?php /*echo $boardId;*/ ?>&dishId=<?php /*echo $item['id'];*/ ?>"><?php /*echo $item['dish_name'] . " ￥" . $item['price'] . '' */ ?></a>-->
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="./js/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="./js/bootstrap.min.js"></script>
</body>
</html>